#!/bin/bash

filepath="/usr/local/docker"
declare -A templates
templates["nginx1.22.1"]="#s
  nginx:
    container_name: nginx
    image: magein/nginx:1.22.1
    restart: always
    ports:
      - 80:80
      - 443:443
      - 8000:8000
    volumes:
      - /usr/local/docker/conf/nginx/1.22.1/nginx.conf:/etc/nginx/nginx.conf
      - /usr/local/docker/conf/nginx/1.22.1/conf.d:/etc/nginx/conf.d
      - /usr/local/docker/html:/usr/share/nginx/html
      - /var/log/docker/nginx/1.22.1:/var/log/nginx
      # 映射证书路径
      #- /etc/letsencrypt:/etc/letsencrypt
      - /www/php:/www/php
      - /www/html:/www/html
      - /www/node:/www/node
      - /www/python:/www/python
      - /www/go:/www/go
    networks:
      webnet:
        ipv4_address: 192.168.10.3
"
templates['nginx']=${templates["nginx1.22.1"]}

templates["php7.4.33"]="#s
  php:
    container_name: php
    image: magein/php-fpm-product:7.4.33
    restart: always
    ports:
      - 9000:9000
    volumes:
      - /usr/local/docker/conf/php/7.4.33/php.ini:/usr/local/etc/php/php.ini
      - /usr/local/docker/conf/php/7.4.33/php-fpm.conf:/usr/local/etc/php-fpm.conf
      - /usr/local/docker/conf/php/7.4.33/php-fpm.d:/usr/local/etc/php-fpm.d
      - /usr/local/docker/html:/usr/share/nginx/html
      - /var/log/docker/php/7.4.33:/usr/local/var/log
      # supervisord配置，使用的话请创建目录以及文件
      #- /usr/local/docker/conf/supervisor/4.2.4/supervisord.conf:/etc/supervisord.conf
      #- /usr/local/docker/conf/supervisor/4.2.4/supervisor.d/:/etc/supervisor.d/
      # 定时任务目录
      #- /var/log/docker/crond/php/7.4.33/:/etc/periodic/
      - /www/php:/www/php
    networks:
      webnet:
        ipv4_address: 192.168.10.21
"
templates["php8.1.13"]="#s
  php:
    container_name: php
    image: magein/php-fpm-alpine3.6:8.1.13
    restart: always
    ports:
      - 9000:9000
    volumes:
      - /usr/local/docker/conf/php/8.1.13/php.ini:/usr/local/etc/php/php.ini
      - /usr/local/docker/conf/php/8.1.13/php-fpm.conf:/usr/local/etc/php-fpm.conf
      - /usr/local/docker/conf/php/8.1.13/php-fpm.d:/usr/local/etc/php-fpm.d
      - /usr/local/docker/html:/usr/share/nginx/html
      - /var/log/docker/php/8.1.13:/usr/local/var/log
      # supervisord配置，使用的话请创建目录以及文件
      #- /usr/local/docker/conf/supervisor/4.2.4/supervisord.conf:/etc/supervisord.conf
      #- /usr/local/docker/conf/supervisor/4.2.4/supervisor.d/:/etc/supervisor.d/
      # 定时任务目录
      #- /var/log/docker/crond/php/8.1.13/:/etc/periodic/
      - /www/php:/www/php
    networks:
      webnet:
        ipv4_address: 192.168.10.21
"
templates['php']=${templates["php7.4.33"]}


templates["redis6.2.8"]="#s
  redis:
    container_name: redis
    image: redis:6.2.8
    restart: always
    ports:
      - 6379:6379
    volumes:
     - /usr/local/docker/conf/redis/6.2.8/redis.conf:/usr/local/redis/redis.conf
     - /usr/local/databse/redis/6.2.8/data:/usr/local/redis/data
     # redis默认不开启日志，官方的镜像中没有生成记录日志的文件，导致启动的时候出现Can't open the log file
     # 如果要日志文件，需要自定义镜像，生成/var/log/redis/redis.conf文件，然后在配置文件中配置log_file
     #- /var/log/docker/redis/6.2.8:/var/log/redis
    command: redis-server /usr/local/redis/redis.conf
    networks:
      webnet:
        ipv4_address: 192.168.10.11
"

templates["redis7.0.7"]="#s
  redis:
    container_name: redis
    image: redis:7.0.7
    restart: always
    ports:
      - 6379:6379
    volumes:
      - /usr/local/docker/conf/redis/7.0.7/redis.conf:/usr/local/redis/redis.conf
      - /usr/local/databse/redis/7.0.7/data:/usr/local/redis/data
      # redis默认不开启日志，官方的镜像中没有生成记录日志的文件，导致启动的时候出现Can't open the log file
      # 如果要日志文件，需要自定义镜像，生成/var/log/redis/redis.conf文件，然后在配置文件中配置log_file
      #- /var/log/docker/redis/6.2.8:/var/log/redis
    command: redis-server /usr/local/redis/redis.conf
    networks:
      webnet:
        ipv4_address: 192.168.10.11
"
templates['redis']=${templates["redis7.0.7"]}

templates["mysql5.7.40"]="#s
  mysql:
    container_name: mysql
    image: mysql:5.7.40
    restart: always
    ports:
      - 3306:3306
    # mysql5.7.40的配置文件默认放在/etc/mysql/mysql
    volumes:
      - /usr/local/docker/conf/mysql/5.7.40/my.cnf:/etc/mysql/my.cnf
      - /usr/local/docker/conf/mysql/5.7.40/conf.d:/etc/mysql/conf.d
      - /usr/local/databse/mysql/5.7.40/data:/var/lib/mysql
      - /var/log/docker/mysql/5.7.40:/var/log/mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      # 这使用-后面要带等号或者使用下面的形式
      # MYSQL_ROOT_PASSWORD:admin123
      - MYSQL_ROOT_PASSWORD=admin123
    networks:
      webnet:
        ipv4_address: 192.168.10.13
"
templates['mysql']=${templates["mysql5.7.40"]}

templates["mysql8.0.31"]="#s
  mysql:
    container_name: mysql
    image: mysql:8.0.31
    restart: always
    ports:
      - 3306:3306
    # mysql8.0.31的配置文件默认放在/etc/my.cnf
    # 通过本地构建的在/etc/mysql/my.cnf
    volumes:
      - /usr/local/docker/conf/mysql/8.0.31/my.cnf:/etc/my.cnf
      - /usr/local/docker/conf/mysql/8.0.31/conf.d:/etc/mysql/conf.d
      - /usr/local/databse/mysql/8.0.31/data:/var/lib/mysql
      - /var/log/docker/mysql/8.0.31:/var/log/mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      # 这使用-后面要带等号或者使用下面的形式
      # MYSQL_ROOT_PASSWORD:admin123
      MYSQL_ROOT_PASSWORD=admin123
    networks:
      webnet:
        ipv4_address: 192.168.10.13
"

templates["mariadb10.4.25"]="#s
  mariadb:
    container_name: mariadb
    image: mariadb:10.4.25
    restart: always
    ports:
      - 3307:3307
    volumes:
      - /usr/local/databse/mariadb/data:/var/lib/mysql
      - /usr/local/docker/config/mariadb/10.4.25/my.conf:/etc/my.conf
      - /usr/local/docker/config/mariadb/10.4.25/conf.d:/etc/mysql/conf.d
      - /var/log/docker/mariadb/10.4.25:/var/log/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=admin123
    networks:
      webnet:
        ipv4_address: 192.168.10.15
"
templates['mariadb']=${templates["mariadb10.4.25"]}

templates["mariadb10.8.6"]="#s
  mariadb:
    container_name: mariadb
    image: mariadb:10.8.6
    restart: always
    ports:
      - 3307:3307
    volumes:
      - /usr/local/databse/mariadb/data:/var/lib/mysql
      - /usr/local/docker/config/mariadb/10.8.6/my.conf:/etc/my.conf
      - /usr/local/docker/config/mariadb/10.8.6/conf.d:/etc/mysql/conf.d
      - /var/log/docker/mariadb/10.8.6:/var/log/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=admin123
    networks:
      webnet:
        ipv4_address: 192.168.10.15
"

templates["mongo5.0.14"]="#s
  mongo:
    container_name: mongo
    image: mongo:5.0.14
    restart: always
    ports:
      - 27017:27017
    volumes:
      - /usr/local/docker/conf/mongo/5.0.14/mongo.conf:/etc/mongod.conf
      - /usr/local/databse/mongo/5.0.14/data:/data/db
      - /var/log/docker/mongo/5.0.14:/var/log/mongodb/
    environment:
      MONGO_INITDB_ROOT_USERNAME: mongo
      MONGO_INITDB_ROOT_PASSWORD: mongo
    networks:
      webnet:
        ipv4_address: 192.168.10.17
"
templates['mongo']=${templates["mongo5.0.14"]}

templates["mongo6.0.3"]="#s
  mongo:
    container_name: mongo
    image: mongo:6.0.3
    restart: always
    ports:
      - 27017:27017
    volumes:
      - /usr/local/docker/conf/mongo/6.0.3/mongo.conf:/etc/mongod.conf
      - /usr/local/databse/mongo/6.0.3/data:/data/db
      - /var/log/docker/mongo/6.0.3:/var/log/mongodb/
    environment:
      MONGO_INITDB_ROOT_USERNAME: mongo
      MONGO_INITDB_ROOT_PASSWORD: mongo
    networks:
      webnet:
        ipv4_address: 192.168.10.17
"

templates["golang1.19.4"]="#s
  golang:
    container_name: go
    image: golang:latest
    restart: always
    ports:
      - 7001-7009:7001-7009
    # stdin_open相当于 run 命令中的 -d
    stdin_open: true
    # tty 相当于 run 命令中的 -i
    tty: true
    volumes:
      - /www/golang/:/www/
    networks:
      webnet:
        ipv4_address: 192.168.10.23
"
templates['golang']=${templates["golang1.19.4"]}

templates["node14.21.2"]="#s
  node:
    container_name: node
    image: node:14.21.2
    restart: always
    ports:
      - 7010-7019:7010-7019
    stdin_open: true
    tty: true
    volumes:
      - /www/node/:/www/node
    networks:
      webnet:
        ipv4_address: 192.168.10.25
"

templates["node18.12.1"]="#s
  nodejs:
    container_name: node
    image: node:18.12.1
    restart: always
    ports:
      - 7010-7019:7010-7019
    restart: always
    stdin_open: true
    tty: true
    volumes:
      - /www/node/:/www/node
    networks:
      webnet:
        ipv4_address: 192.168.10.25
"
templates['node']=${templates["node18.12.1"]}

format(){
    printf "%-30s%-10s" "$1" "$2"
    echo ""
}

printError(){

    echo "Error:"
    for item in "$@"
    do
        echo -e "   $item"
    done
    exit 1
}

usage() {
    echo "Usage:"
    format "-h,--help" "get help"
    format "-o" "write content to FILE,default ${filepath}"
    echo "Example:"
    echo "sudo docker-make-compose nginx"
    echo "sudo docker-make-compose -o /usr/local/ nginx"
    echo "sudo docker-make-compose nginx php"
}

support(){
  for key in "${!templates[@]}"
  do
    echo "$key"
  done
}

eval set -- "$(getopt -q -o "ho:n:" -l help,support,name: -- "$@")"

while [ -n "$1" ]; do
    case $1 in
    -h|--help)
        usage
        exit 0
        ;;
    -o)
        filepath="$2"
        shift 2
        break
        ;;
    --support)
        support
        exit 0
        ;;
    --)
        shift 1
        break
        ;;
    *)
        break
        ;;
    esac
done

# 删除第一个--
if [ "$1" = "--" ]
then
  shift 1
fi

services=( "$@" )

if [ ! -d "$filepath" ]
then
  printError "Please specify the input file path"
fi
filepath=$(echo "$filepath" | sed "s/[\/]\+$//")

if [ ${#services[@]} -eq 0 ]
then
    usage
    exit 0
fi

filename="${filepath}/docker-compose.yml"

if [ -f "$filename" ]
then

    content=$(cat "$filename")
    if [ -n "$content" ]
    then
      echo "$filename exist , Failed"
      exit 0
    fi
fi

sudo touch "$filename"
sudo chmod 777 "$filename"

# 排序且去重
# 使用sed将数组处理成一行一行的，在使用sort命令排序且去除重复
# 最外面的大括号是将结果保存成数组
unique_services=( $(echo "${services[@]}" | sed -e "s/ /\n/g" | sort -fu | sed -e "s/\n//g") )

$(sudo echo -e "#docker compose file, version3 version: \"3\"\nservices:" >"$filename")

# 防止重复的服务
for item in "${unique_services[@]}"
do
    content=${templates[$item]}
    content=$(sudo echo "${content}" | sed "s/#s/  # ${item} service/g")
    $(sudo echo -e "${content}" >> "$filename")
done

wennet="#Web Virtual Internet
networks:
  webnet:
    name: webnet
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.10.0/24
          gateway: 192.168.10.1
"

$(sudo echo -e "${wennet}" >> "$filename")

if [ $? -eq 0 ]; then
    echo "compose.yml create completed"
    echo "You can use the following command to start"
    echo "docker compose -f ${filename} up -d"
fi
